<template>
  <div class="container" ref="mainCon">
    <div v-if="loginType != 'project'">
      <topTitle titleText="项目权限"></topTitle>
      <div style="position: absolute; right: 24px; top: 7px">
        <el-button
          class="commonBut saveBtn funComBut"
          icon="icon iconfont icontianjia"
          @click="toAddEmployee"
          v-show="isShowAdd"
        >
          <span class="opetareText">新增账号</span>
        </el-button>
      </div>
    </div>
    <div class="topBox" v-else>
      <el-select
        v-model="filterField.projectId"
        filterable
        placeholder="暂无关联项目"
        @change="changeProject"
      >
        <el-option
          v-for="(item, index) of projectList"
          :key="index"
          :label="item.name"
          :value="item.code"
        ></el-option>
      </el-select>
      <!-- <el-button type="primary" icon="icon iconfont icontianjia" class="searchBtn alignRight" @click="toAddEmployee" v-show="isShowAdd">
                <span style="vertical-align: text-top;">
                    新增账号
                </span>
            </el-button> -->
      <el-button
        class="commonBut saveBtn funComBut alignRight"
        icon="icon iconfont icontianjia"
        @click="toAddEmployee"
        v-show="isShowAdd"
      >
        <span class="opetareText">新增账号</span>
      </el-button>
    </div>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="姓名" class="row-padding-bottom">
              <el-input
                placeholder="请输入姓名"
                clearable
                v-model="filterField.nickname"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="登录账号" class="row-padding-bottom">
              <el-input
                placeholder="请输入登录账号"
                clearable
                v-model="filterField.username"
              ></el-input>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="岗位" class="row-padding-bottom">
                <el-select
                  v-model="filterField.roleCode"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in roleCodeOptions"
                    :key="item.id"
                    :label="item.text"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="账号状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.enabled"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in stateOptions"
                    :key="item.id"
                    :label="item.text"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
              <exportBtn
                :dataLen="projectRightList.length"
                :filterField="filterField"
                :api="api"
              ></exportBtn>
            </el-form-item>
          </el-col>
          <!-- <el-form-item>
                        <el-button type="primary" icon="icon iconfont iconchazhao1" @click="searchList" class="commonBtn"></el-button>
                    </el-form-item> -->
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="projectRightList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="username"
            label="登录账号"
            min-width="230"
            align="left"
            :formatter="formatTd"
          ></el-table-column>
          <!-- <el-table-column prop="nickname" label="姓名" align="center" :formatter='formatTd'></el-table-column> -->
          <el-table-column prop="nickname" label="姓名" min-width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.nickname"
                :columnName="'nickname'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="roleName"
            label="岗位"
            min-width="230"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="enabled"
            label="账号状态"
            min-width="230"
            align="center"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <p v-if="scope.row.enabled == '0'">禁用</p>
              <p v-else-if="scope.row.enabled == '1'">正常</p>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="left" fixed="right" width="110">
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-if="isShowDetail"
                @click="toDetail(scope.$index, scope.row)"
                >账号管理</el-link
              >
              <span v-else>--</span>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关项目权限数据"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
  </div>
</template>
<script>
import scrollTable from '@/mixin/scrollTable.js'

export default {
  name: 'projectRightList',
  mixins: [scrollTable],
  data() {
    return {
      loginType: '',
      advanced: false,
      roleCodeOptions: [
        { id: 'PROJECT_MANAGER', text: '项目管理员' },
        { id: 'LABOR_ADMINISTRATOR', text: '劳资专管员' },
      ],
      stateOptions: [
        { id: '1', text: '正常' },
        { id: '0', text: '禁用' },
      ],
      //表格数据
      projectRightList: [],
      requestUrl: 'api-u/users/subaccounts',
      //搜索条件
      filterField: {
        nickname: '', //姓名
        username: '', //登录账号
        roleCode: '', //岗位
        enabled: '', //账号状态
        projectId: '',
      },
      projectList: [],
      pageSize: 20,
      pageNo: 1,
      isShow: false, //列表加载
      loading: true,
      isShowAdd: '',
      isShowDetail: '',
      api: '',
    }
  },
  created() {
    this.getApi()
    this.isShowAdd =
      this.$store.state.buttonLimitsArr.indexOf('projectAuth:add') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('projectAuthSGF:add') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('projectUserList:add') > -1
    this.isShowDetail =
      this.$store.state.buttonLimitsArr.indexOf('projectAuth:detail') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('projectAuthSGF:detail') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('projectUserList:detail') > -1
  },
  async mounted() {
    if (
      this.$store.state.userType == 'PROJECT_ADMINISTRATOR' ||
      this.$store.state.userType == 'LABOR_ADMINISTRATOR'
    ) {
      this.loginType = 'project'
      ;(this.roleCodeOptions = [
        { id: 'PROJECT_MANAGER', text: '项目管理员' },
        { id: 'LABOR_ADMINISTRATOR', text: '劳资专管员' },
        { id: 'FENBAO_MANAGER', text: '分包负责人' },
      ]),
        await this.getProjectList()
    }
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    // 获取项目列表
    getProjectList() {
      return this.axios({
        method: 'post',
        url: 'api-p/projectcompany/expand/current/projects',
      })
        .then((response) => {
          if (response.data.hasError) {
            this.$message.error(response.data.errorMsg)
          } else {
            const results = response.data.result
            this.projectList = results
            this.filterField.projectId = this.projectList[0].code
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 切换项目
    changeProject(val) {
      this.filterField.projectId = val
      this.$refs.pagination.getTableListData()
    },
    getTableList(data) {
      this.projectRightList = data
      this.loading = false
      if (this.projectRightList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //添加账号
    toAddEmployee() {
      let pathObj = {
        path: '/home/projectRight/addProjectRight',
        name: 'addProjectRight',
      }
      if (this.loginType == 'project') {
        pathObj.query = {
          projectId: this.filterField.projectId,
          projectName: this.projectList.find(
            (v) => v.code == this.filterField.projectId
          ).name,
        }
      }
      this.$router.push(pathObj)
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/projectRight/projectRightList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    //账号详情
    toDetail(index, row) {
      let pathObj = {
        path: '/home/projectRight/projectRightDetail',
        name: 'projectRightDetail',
        query: {
          id: row.id,
        },
      }
      if (this.loginType == 'project') {
        pathObj.query.projectId = this.filterField.projectId
        pathObj.query.projectName = this.projectList.find(
          (v) => v.code == this.filterField.projectId
        ).name
      }
      this.$router.push(pathObj)
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/projectRight/projectRightList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    getApi() {
      if (localStorage.getItem('loginType') === 'QY') {
        this.api = 'api-s/report/export_QY_SUB_ACCOUNTS'
      } else if (localStorage.getItem('loginType') === 'XM') {
        this.api = 'api-s/report/export_XM_SUB_ACCOUNTS'
      }
    },
  },
}
</script>

<style lang="less" scoped>
@import '../../../static/css/filterArea.less';
.container {
  .topContent {
    padding: 0 24px;
    height: 63px;
    line-height: 63px;
    .titleTop {
      font-size: 18px;
      color: #333333;
      font-weight: bold;
      height: 30px;
      line-height: 30px;
    }
    .alignRight {
      text-align: right;
      position: absolute;
      right: 0px;
      top: 9px;
    }

    /deep/.el-input__inner {
      font-size: 22px;
      font-weight: bold;
      color: #333333;
      border: none;
      height: 22px;
      // background: #e4ebf1;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      padding-left: 0;
    }
    /deep/.el-select__caret {
      color: #333;
      font-size: 20px;
      line-height: 31px;
    }
  }
  .mainCon {
    top: 198px;
  }
  .createSalaryBut {
    background: #009588;
    border: 1px solid #009588;
    padding: 9px 23px;
    &:hover {
      opacity: 0.8;
    }
  }
}
.topBox {
  position: relative;
  padding: 17px 24px 17px 10px;
  &::after {
    position: absolute;
    right: -0px;
    top: 64px;
    left: -0px;
    height: 1px;
    content: '';
    -webkit-transform: scaleY(0.5);
    transform: scaleY(0.5);
    background-color: #e3e7e9;
    z-index: 10;
  }
  /deep/.el-input__inner {
    width: 346px;
    font-size: 24px;
    font-weight: bold;
    color: #333333;
    border: none;
    height: 30px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /deep/.el-select__caret {
    color: #333 !important;
    font-size: 20px !important;
    line-height: 30px !important;
  }
  .alignRight {
    // width: 136px;
    // height: 36px;
    position: absolute;
    top: 50%;
    right: 24px;
    transform: translateY(-50%);
  }
}
</style>
